<template>
  <el-dialog :title="!dataForm.bargainId ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      {{dataForm}}
      <!-- <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item> -->
      <el-form-item label="商品名称"
                    prop="goodsName">
        <el-select v-model="goodsIndex"
                   placeholder="请选择">
          <el-option v-for="(item,index) in goods"
                     :key="item.id"
                     :label="item.goodsName"
                     :value="index">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="原价"
                    prop="goodsPriced">
        <el-input v-model="dataForm.goodsPriced"
                  placeholder="原价"
                  disabled
                  style="width: 250px;"></el-input>
      </el-form-item>
      <el-form-item label="低价"
                    prop="goodsPrice">
        <el-input v-model="dataForm.goodsPrice"
                  placeholder="低价"
                  style="width: 250px;"></el-input>
      </el-form-item>
      <el-form-item label="开始时间"
                    prop="startDate">
        <el-date-picker v-model="dataForm.startDate"
                        type="datetime"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间"
                    prop="endDate">
        <el-date-picker v-model="dataForm.endDate"
                        type="datetime"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="砍价规则"
                    prop="ruleFlag">
        <el-radio v-model="dataForm.ruleFlag"
                  label="0">共<el-input v-model="dataForm.ruleNum"
                    placeholder="砍价"
                    style="width: 150px;"></el-input>刀砍至低价</el-radio><br>
        <el-radio v-model="dataForm.ruleFlag"
                  label="1">每刀最多可砍<el-input v-model="dataForm.ruleNum"
                    placeholder="砍价"
                    style="width: 150px;"></el-input>元</el-radio>
      </el-form-item>
      <el-form-item label="活动描述"
                    prop="activityDesc">
        <el-input v-model="dataForm.activityDesc"
                  placeholder="活动描述"></el-input>
      </el-form-item>
      <el-form-item label="活动库存"
                    prop="stockCount">
        <el-input v-model="dataForm.stockCount"
                  placeholder="活动库存"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      goodsIndex: '',
      goods: [],
      visible: false,
      dataForm: {
        bargainId: 0,
        goodsId: '',
        goodsName: '',
        goodsPriced: '',
        goodsPrice: '',
        startDate: '',
        endDate: '',
        ruleNum: '',
        ruleFlag: '',
        activityDesc: '',
        stockCount: ''
      },
      dataRule: {
        goodsId: [
          { required: true, message: '商品id不能为空', trigger: 'blur' }
        ],
        goodsName: [
          { required: true, message: '商品名称不能为空', trigger: 'blur' }
        ],
        goodsPriced: [
          { required: true, message: '原价不能为空', trigger: 'blur' }
        ],
        goodsPrice: [
          { required: true, message: '低价不能为空', trigger: 'blur' }
        ],
        startDate: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endDate: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ],
        ruleNum: [
          { required: true, message: '砍价规则不能为空', trigger: 'blur' }
        ],
        ruleFlag: [
          { required: true, message: '砍价规则标识不能为空', trigger: 'blur' }
        ],
        activityDesc: [
          { required: true, message: '活动描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    'goodsIndex' (val) {
      console.log(val, this.goods[val])
      this.dataForm.goodsId = this.goods[val].id
      this.dataForm.goodsName = this.goods[val].goodsName
      this.dataForm.goodsPriced = this.goods[val].goodsPrice
    }
  },
  created () {
    this.getGoods()
  },
  methods: {
    getGoods () {
      this.$http({
        url: this.$http.adornUrl(`/goods/goods/list`),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.goods = data.page.list
        }
      })
    },
    init (id) {
      this.dataForm.bargainId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.bargainId) {
          this.$http({
            url: this.$http.adornUrl(`/bargain/bargainactivity/info/${this.dataForm.bargainId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.goodsId = data.bargainActivity.goodsId
              this.dataForm.goodsName = data.bargainActivity.goodsName
              this.dataForm.goodsPriced = data.bargainActivity.goodsPriced
              this.dataForm.goodsPrice = data.bargainActivity.goodsPrice
              this.dataForm.startDate = data.bargainActivity.startDate
              this.dataForm.endDate = data.bargainActivity.endDate
              this.dataForm.ruleNum = data.bargainActivity.ruleNum
              this.dataForm.ruleFlag = data.bargainActivity.ruleFlag
              this.dataForm.activityDesc = data.bargainActivity.activityDesc
              this.dataForm.stockCount = data.bargainActivity.stockCount
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/bargain/bargainactivity/${!this.dataForm.bargainId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'bargainId': this.dataForm.bargainId || undefined,
              'goodsId': this.dataForm.goodsId,
              'goodsName': this.dataForm.goodsName,
              'goodsPriced': this.dataForm.goodsPriced,
              'goodsPrice': this.dataForm.goodsPrice,
              'startDate': this.dataForm.startDate,
              'endDate': this.dataForm.endDate,
              'ruleNum': this.dataForm.ruleNum,
              'ruleFlag': this.dataForm.ruleFlag,
              'activityDesc': this.dataForm.activityDesc,
              'stockCount': this.dataForm.stockCount
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
